<?php

namespace App\Http\Controllers;

use App\Models\Setting;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Validator;

class AdminController extends Controller
{
    use DispatchesJobs;

    protected   $menu;
    protected   $verifyFactor = [];
    protected   $user;
    protected   $adminGroup;
    /**
     * 后台公共类
     */
    public function __construct(Request $request)
    {
        $this->menu = $request->menu;
        $this->user = $request->user;
        $this->adminGroup = $request->group;
        $this->getAdminGlobal();
        $this->getMenu();
    }

    /**
     * 获取后台局部变量
     */
    private function getAdminGlobal()
    {
        //获取后台设置
        $SETTING_ADMIN = Cache::remember(config('cache_name.setting_admin'), 1440, function()
        {
            return Setting::where('type', 'system')->get()->keyBy('key');
        });

        View::share(['SETTING_ADMIN' => $SETTING_ADMIN]);
    }

    /**
     * 获取目录
     */
    private function getMenu()
    {
        //获取横向目录
        $HMENU = array_where($this->menu, function($key, $value)
        {
            return isset($value->active) && $value->active == 1;
        });
        //排序
        $HMENU = array_values(array_sort($HMENU, function($value)
        {
            return $value;
        }));
        //获取一级栏目
        $FMENU = array();
        foreach ($this->menu as $v)
        {
            if ($v->parentID == 0)
            {
                $v->url = '';
                if (!empty($v->controller))
                {
                    $v->url = action($v->controller);//获取URL
                }
                $FMENU[] = $v;
            }
        }
        //获取二级栏目
        $SMENU = array();
        foreach ($FMENU as $v)
        {
            $SMENU[$v->id] = array();
            foreach ($this->menu as $m)
            {
                if ($m->parentID == $v->id && $m->display == 1)
                {
                    $m->url = '';
                    if (!empty($m->controller))
                    {
                        $m->url = action($m->controller);//获取URL
                    }
                    $SMENU[$v->id][] = $m;
                }
            }
        }

        View::share(['FMENU'=>$FMENU,'SMENU'=>$SMENU,'HMENU'=>$HMENU,'USER'=>$this->user,'GROUP'=>$this->adminGroup]);
    }

    /**
     * 表单验证
     * @param array $inputData
     * @param array $library
     * @return bool
     */
    protected function verification($inputData, $library = [])
    {
        //当前类所有验证
        $library = array_merge($this->verifyFactor, $library);
        //获取当前动作验证
        $verifyArr = [];
        foreach ($library as $k=>$v)
        {
            if (isset($inputData[$k]))
            {
                $verifyArr[$k] = $v;
            }
        }

        $v = Validator::make($inputData,$verifyArr);

        if ($v->fails())
        {
            return $v->errors();
        }
        return false;
    }
}
